﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using OOAD_Project_Engine.Domain.Entities;
using OOAD_Project_Engine.Presentation;
using OOAD_Project_Engine.Foundation.Interfaces;

namespace OOAD_Project_Engine.Foundation
{
    class CoordinateHelper
    {

        /// <summary>
        /// Get a matrix to transform local coordinate to parent coordinate.</summary>
        /// <param name="entity">
        /// entity contains information of position,rotation and scale.</param>
        /// <returns>A matrix that can transform entity local coordinate to screen coordinate.</returns>
        public static Matrix ProjectionMatrix(float rotation, Vector2 position, Vector2 orginSize, Vector2 scale)
        {
            Matrix translateToOrigin = Matrix.CreateTranslation(-orginSize.X, -orginSize.Y, 0);
            Matrix rotationMatrix = Matrix.CreateRotationZ(rotation);
            Matrix scaleMatrix = Matrix.CreateScale(new Vector3(scale.X, scale.Y, 0));
            Matrix translateBackToPosition = Matrix.CreateTranslation(position.X, position.Y, 0);

            Matrix compositeMatrix = translateToOrigin * scaleMatrix * rotationMatrix * translateBackToPosition;//先縮放再旋轉
            return compositeMatrix;
        }

        /// <summary>
        /// Get a matrix to transform parent coordinate to child coordinate.</summary>
        /// <param name="world">
        /// entity contains information of position,rotation and scale.</param>
        /// <returns>A matrix that can transform parent entity coordinate to child coordinate.</returns>
        public static Matrix UnprojectionMatrix(float rotation, Vector2 entityPos, Vector2 orginSize, Vector2 scale)
        {
            Matrix backToOriginTransform = Matrix.CreateTranslation(-entityPos.X, -entityPos.Y, 0);
            Matrix rotationTransform = Matrix.CreateRotationZ(-rotation);
            Matrix scaleTransform = Matrix.CreateScale(new Vector3(1f / scale.X, 1f / scale.Y, 0));
            Matrix posTransform = Matrix.CreateTranslation(orginSize.X, orginSize.Y, 0);

            Matrix finalTransform;
            finalTransform = backToOriginTransform * rotationTransform * scaleTransform * posTransform;//先旋轉再縮放

            return finalTransform;
        }


    }
}
